
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>深入理解jvm之内存区域与内存溢出 | wustrive&#39;s blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="wustrive">
    
    <meta name="description" content="Java内存区域与内存溢出异常运行时数据区域
程序计数器
当前线程所执行的字节码的行号指示器
当前线程私有
不会出现OutOfMemoryError情况

java虚拟机栈
线程私有，生命周期与线程相同
java方法执行的内存模型，每个方法执行的同时都会创建一个栈帧，存储局部变量表(基本类型、对象引">
    
    
    
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:site" content="wustrive" />
    <meta name="twitter:title" content="深入理解jvm之内存区域与内存溢出 | wustrive&#39;s blog" />
      
    
    
    <link rel="alternate" href="/atom.xml" title="wustrive&#39;s blog" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/logo.ico">
    <link rel="apple-touch-icon-precomposed" href="/img/logo.ico">
    
    <link rel="stylesheet" href="/css/style.css">
    <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?5829c94e2ff91660826b6c0c188f5620";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.png" alt="wustrive&#39;s blog" title="wustrive&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="wustrive&#39;s blog">wustrive&#39;s blog</a></h1>
				<h2 class="blog-motto">好记性不如写博客</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">主页</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/aboutme">关于</a></li>
					
					<li>
					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="text" id="search" name="q" autocomplete="off" maxlength="20" placeholder="搜索" />
						<input type="hidden" name="q" value="site:wustrive2008.github.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2015/12/24/java/深入理解jvm之内存区域与内存溢出/" title="深入理解jvm之内存区域与内存溢出" itemprop="url">深入理解jvm之内存区域与内存溢出</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://wustrive2008.github.io" title="wustrive">wustrive</a>
    </p>
  <p class="article-time">
    <time datetime="2015-12-24T13:21:29.000Z" itemprop="datePublished">2015-12-24</time>
    更新日期:<time datetime="2016-11-05T18:14:33.000Z" itemprop="dateModified">2016-11-06</time>
    
  </p>
</header>
	<div class="article-content">
		
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-hexojava知识结构.png" rel="gallery_civ5jdlh2001ruvfybe2a8rq2">
        <img src="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-hexojava知识结构.png" itemprop="image">
      </a>
    
  </div>
</div>

		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Java内存区域与内存溢出异常"><span class="toc-number">1.</span> <span class="toc-text">Java内存区域与内存溢出异常</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#运行时数据区域"><span class="toc-number">1.1.</span> <span class="toc-text">运行时数据区域</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#程序计数器"><span class="toc-number">1.1.1.</span> <span class="toc-text">程序计数器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#java虚拟机栈"><span class="toc-number">1.1.2.</span> <span class="toc-text">java虚拟机栈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#本地方法栈"><span class="toc-number">1.1.3.</span> <span class="toc-text">本地方法栈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Java堆(Java_Heap)"><span class="toc-number">1.1.4.</span> <span class="toc-text">Java堆(Java Heap)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#方法区"><span class="toc-number">1.1.5.</span> <span class="toc-text">方法区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#运行时常量池"><span class="toc-number">1.1.6.</span> <span class="toc-text">运行时常量池</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#直接内存"><span class="toc-number">1.1.7.</span> <span class="toc-text">直接内存</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HotSpot虚拟机"><span class="toc-number">1.2.</span> <span class="toc-text">HotSpot虚拟机</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#对象的创建"><span class="toc-number">1.2.1.</span> <span class="toc-text">对象的创建</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#对象的访问定位"><span class="toc-number">1.2.2.</span> <span class="toc-text">对象的访问定位</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OOM异常的解决思路"><span class="toc-number">1.3.</span> <span class="toc-text">OOM异常的解决思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#捐赠"><span class="toc-number">1.5.</span> <span class="toc-text">捐赠</span></a></li></ol></li></ol>
		</div>
		
		<h2 id="Java内存区域与内存溢出异常">Java内存区域与内存溢出异常</h2><h3 id="运行时数据区域">运行时数据区域</h3><p><img src="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-blog%2Fjava%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%86%85%E5%AD%98%E5%88%86%E5%8C%BA.jpg" alt="java虚拟机内存分区"></p>
<h4 id="程序计数器">程序计数器</h4><ul>
<li>当前线程所执行的字节码的行号指示器</li>
<li>当前线程私有</li>
<li>不会出现OutOfMemoryError情况</li>
</ul>
<h4 id="java虚拟机栈">java虚拟机栈</h4><ul>
<li>线程私有，生命周期与线程相同</li>
<li>java方法执行的内存模型，每个方法执行的同时都会创建一个栈帧，存储局部变量表(基本类型、对象引用)、操作数栈、动态链接、方法出口等信息</li>
<li>StackOverflowError异常：当线程请求的栈深度大于虚拟机所允许的深度</li>
<li>OutOfMemoryError异常：如果栈的扩展时无法申请到足够的内存</li>
</ul>
<h4 id="本地方法栈">本地方法栈</h4><p>与虚拟机栈相似，主要为虚拟机使用到的Native方法服务，在HotSpot虚拟机中直接把本地方法栈与虚拟机栈二合一</p>
<h4 id="Java堆(Java_Heap)">Java堆(Java Heap)</h4><p>java堆是被所有线程共享的一块内存区域，在虚拟机启动时创建。此区域的唯一目的就是存储对象实例。java堆是垃圾收集器管理的主要区域。java堆还可以细分为：新生代与老年代。在细一点有Eden空间、Form Survivor空间、To Survivor空间等。</p>
<ul>
<li>可以通过-Xmx和-Xms控制堆的大小</li>
<li>OutOfMemoryError异常：当在堆中没有内存完成实例分配，且堆也无法再扩展时。</li>
</ul>
<h4 id="方法区">方法区</h4><ul>
<li>线程间共享</li>
<li>用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据</li>
<li>OutOfMemoryError异常：当方法区无法满足内存的分配需求时</li>
</ul>
<h4 id="运行时常量池">运行时常量池</h4><ul>
<li>方法区的一部分</li>
<li>用于存放编译期生成的各种字面量与符号引用</li>
<li>OutOfMemoryError异常：当常量池无法再申请到内存时</li>
</ul>
<h4 id="直接内存">直接内存</h4><ul>
<li>NIO可以使用Native函数库直接分配堆外内存，堆中的DirectByteBuffer对象作为这块内存的引用进行操作</li>
<li>大小不受Java堆大小的限制，受本机(服务器)内存限制</li>
<li>OutOfMemoryError异常：系统内存不足时</li>
</ul>
<h3 id="HotSpot虚拟机">HotSpot虚拟机</h3><h4 id="对象的创建">对象的创建</h4><p>虚拟机遇到一条new指令时，首先将去检查这个对象的参数是否在常量池中定位到一个类的符号引用，并且检查这个符号引用代表的类是否已被加载、解析和初始化过。如果没有，必须先执行类的加载过程。<br>在类加载检查通过后，虚拟机将为新生对象分配内存。对象所需内存大小再类加载完成后便可确定。内存分配可以采用“指针碰撞”与“空闲列表”的方式。</p>
<h4 id="对象的访问定位">对象的访问定位</h4><p>java程序需要通过栈上的reference数据来操作堆上的具体对象。访问方式有使用句柄和直接指针两种。</p>
<ul>
<li>句柄访问 java堆中将会划分出一块内存来作为句柄池，reference中存储的就是对象的句柄地址，而句柄中包含了对象实例数据与类型数据各自的具体地址信息</li>
<li>直接指针访问 java堆对象的布局中必须考虑如何放置访问类型数据的相关信息，reference中存储的就是对象地址</li>
</ul>
<h3 id="OOM异常的解决思路">OOM异常的解决思路</h3><p>生成Dump快照文件：</p>
<ul>
<li>通过jvm参数—XX:-HeapDumpOnOutOfMemoryError可以让JVM在出现内存溢出是Dump出当前的内存转储快照</li>
<li>用jmap生产dump文件，win通过任务管理器查看tomcat的进程pid，linux用ps命令查看进程pid,然后用jmap命令</li>
</ul>
<p>先通过内存映像分析工具(如Eclipse的Memory Analyzer)进行分析，常见的情况有：</p>
<ul>
<li>内存泄露，对象已经死了，无法通过垃圾收集器进行自动回收，通过找出泄露的代码位置和原因，才好确定解决方案；</li>
<li>内存溢出，内存中的对象都还必须存活着，这说明Java堆分配空间不足，检查堆设置大小（-Xmx与-Xms），检查代码是否存在对象生命周期太长、持有状态时间过长的情况。</li>
</ul>
<p>OOM异常示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">package oom;</div><div class="line"></div><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.List;</div><div class="line"></div><div class="line">/**</div><div class="line"> * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError</div><div class="line">* @ClassName: HeapOOM </div><div class="line">*</div><div class="line"> */</div><div class="line">public class HeapOOM &#123;</div><div class="line">    static class OOMObject&#123;</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    public static void main(String[] args) &#123;</div><div class="line">        List&lt;OOMObject&gt; list = new ArrayList&lt;OOMObject&gt;();</div><div class="line">        while(true)&#123;</div><div class="line">            list.add(new OOMObject());</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="参考">参考</h3><p>《深入理解java虚拟机 JVM高级特性与最佳实践》</p>
<h3 id="捐赠">捐赠</h3><p>如何觉得本文章对你有帮助，欢迎捐赠</p>
<p><img src="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-hexo%E6%94%AF%E4%BB%98%E5%AE%9D%E4%BB%98%E6%AC%BE%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%9C%80%E5%B0%8F.png" alt="捐赠"></p>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/java/">java</a><a href="/tags/虚拟机/">虚拟机</a><a href="/tags/性能/">性能</a><a href="/tags/jvm/">jvm</a><a href="/tags/HotSpot/">HotSpot</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/java/">java</a>
</div>



<div class="article-share" id="share">

  <div data-url="https://wustrive2008.github.io/2015/12/24/java/深入理解jvm之内存区域与内存溢出/" data-title="深入理解jvm之内存区域与内存溢出 | wustrive&#39;s blog" data-tsina="null" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2015/12/25/java/深入理解jvm之垃圾收集器/" title="深入理解jvm之垃圾收集器">
  <strong>PREVIOUS:</strong><br/>
  <span>
  深入理解jvm之垃圾收集器</span>
</a>
</div>


<div class="next">
<a href="/2015/10/24/设计模式/重构之代码的坏味道/"  title="重构之代码的坏味道">
 <strong>NEXT:</strong><br/> 
 <span>重构之代码的坏味道
</span>
</a>
</div>

</nav>

	
<section class="comment">
	<div class="ds-thread"></div>
</section>

</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Java内存区域与内存溢出异常"><span class="toc-number">1.</span> <span class="toc-text">Java内存区域与内存溢出异常</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#运行时数据区域"><span class="toc-number">1.1.</span> <span class="toc-text">运行时数据区域</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#程序计数器"><span class="toc-number">1.1.1.</span> <span class="toc-text">程序计数器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#java虚拟机栈"><span class="toc-number">1.1.2.</span> <span class="toc-text">java虚拟机栈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#本地方法栈"><span class="toc-number">1.1.3.</span> <span class="toc-text">本地方法栈</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Java堆(Java_Heap)"><span class="toc-number">1.1.4.</span> <span class="toc-text">Java堆(Java Heap)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#方法区"><span class="toc-number">1.1.5.</span> <span class="toc-text">方法区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#运行时常量池"><span class="toc-number">1.1.6.</span> <span class="toc-text">运行时常量池</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#直接内存"><span class="toc-number">1.1.7.</span> <span class="toc-text">直接内存</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HotSpot虚拟机"><span class="toc-number">1.2.</span> <span class="toc-text">HotSpot虚拟机</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#对象的创建"><span class="toc-number">1.2.1.</span> <span class="toc-text">对象的创建</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#对象的访问定位"><span class="toc-number">1.2.2.</span> <span class="toc-text">对象的访问定位</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OOM异常的解决思路"><span class="toc-number">1.3.</span> <span class="toc-text">OOM异常的解决思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#捐赠"><span class="toc-number">1.5.</span> <span class="toc-text">捐赠</span></a></li></ol></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">分类</p>
		<ul>
		
			<li><a href="/categories/DB/" title="DB">DB<sup>1</sup></a></li>
		
			<li><a href="/categories/Linux/" title="Linux">Linux<sup>6</sup></a></li>
		
			<li><a href="/categories/UML/" title="UML">UML<sup>1</sup></a></li>
		
			<li><a href="/categories/cORc/" title="cORc++">cORc++<sup>1</sup></a></li>
		
			<li><a href="/categories/hexo/" title="hexo">hexo<sup>1</sup></a></li>
		
			<li><a href="/categories/http协议/" title="http协议">http协议<sup>2</sup></a></li>
		
			<li><a href="/categories/java/" title="java">java<sup>10</sup></a></li>
		
			<li><a href="/categories/js/" title="js">js<sup>1</sup></a></li>
		
			<li><a href="/categories/nginx/" title="nginx">nginx<sup>1</sup></a></li>
		
			<li><a href="/categories/python/" title="python">python<sup>2</sup></a></li>
		
			<li><a href="/categories/分享/" title="分享">分享<sup>1</sup></a></li>
		
			<li><a href="/categories/摘录/" title="摘录">摘录<sup>1</sup></a></li>
		
			<li><a href="/categories/版本控制/" title="版本控制">版本控制<sup>3</sup></a></li>
		
			<li><a href="/categories/算法/" title="算法">算法<sup>2</sup></a></li>
		
			<li><a href="/categories/缓存/" title="缓存">缓存<sup>1</sup></a></li>
		
			<li><a href="/categories/网络协议/" title="网络协议">网络协议<sup>1</sup></a></li>
		
			<li><a href="/categories/网络安全/" title="网络安全">网络安全<sup>1</sup></a></li>
		
			<li><a href="/categories/设计模式/" title="设计模式">设计模式<sup>2</sup></a></li>
		
			<li><a href="/categories/读书笔记/" title="读书笔记">读书笔记<sup>10</sup></a></li>
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">标签</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Arraylist/" title="Arraylist">Arraylist<sup>1</sup></a></li>
		
			<li><a href="/tags/Centos/" title="Centos">Centos<sup>2</sup></a></li>
		
			<li><a href="/tags/DB/" title="DB">DB<sup>1</sup></a></li>
		
			<li><a href="/tags/Docker/" title="Docker">Docker<sup>1</sup></a></li>
		
			<li><a href="/tags/Email/" title="Email">Email<sup>1</sup></a></li>
		
			<li><a href="/tags/HashMap/" title="HashMap">HashMap<sup>1</sup></a></li>
		
			<li><a href="/tags/HotSpot/" title="HotSpot">HotSpot<sup>1</sup></a></li>
		
			<li><a href="/tags/IMAP/" title="IMAP">IMAP<sup>1</sup></a></li>
		
			<li><a href="/tags/Java/" title="Java">Java<sup>1</sup></a></li>
		
			<li><a href="/tags/Linux/" title="Linux">Linux<sup>5</sup></a></li>
		
			<li><a href="/tags/OOM/" title="OOM">OOM<sup>1</sup></a></li>
		
			<li><a href="/tags/POP3/" title="POP3">POP3<sup>1</sup></a></li>
		
			<li><a href="/tags/SMTP/" title="SMTP">SMTP<sup>1</sup></a></li>
		
			<li><a href="/tags/Tomcat/" title="Tomcat">Tomcat<sup>1</sup></a></li>
		
			<li><a href="/tags/UML/" title="UML">UML<sup>1</sup></a></li>
		
			<li><a href="/tags/aes/" title="aes">aes<sup>1</sup></a></li>
		
			<li><a href="/tags/array/" title="array">array<sup>1</sup></a></li>
		
			<li><a href="/tags/arraylist/" title="arraylist">arraylist<sup>1</sup></a></li>
		
			<li><a href="/tags/c/" title="c">c<sup>2</sup></a></li>
		
			<li><a href="/tags/chkconfig/" title="chkconfig">chkconfig<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello, I&#39;m wustrive. This is my blog on GitHub. <br/>
			I wish you happiness</p>
	</section>
	 
	<div class="social-font clearfix">
		
		<a href="http://weibo.com/12100106" target="_blank" title="weibo"></a>
		
		
		<a href="https://twitter.com/wustrive" target="_blank" title="twitter"></a>
		
		
		<a href="https://github.com/wustrive2008" target="_blank" title="github"></a>
		
		
		
	</div>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2018 
		
		<a href="https://wustrive2008.github.io" target="_blank" title="wustrive">wustrive</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>


<script type="text/javascript">
  var duoshuoQuery = {short_name:"wustrive"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 




<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-22190106-2', 'auto');  
ga('send', 'pageview');
</script>


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-22190106-2', 'auto');
  ga('send', 'pageview');

</script>

  </body>
</html>
